package com.onemileshop.net;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.xmlpull.v1.XmlPullParser;

import com.onemileshop.entiy.Session;
import com.onemileshop.entiy.UrlParams;
import com.onemileshop.entiy.model.EMsgHead;
import com.onemileshop.entiy.model.EOrder;
import com.onemileshop.entiy.model.EPostData;
import com.onemileshop.entiy.model.EPostResult;
import com.onemileshop.entiy.model.EShopInfo;
import com.onemileshop.utils.UtiDateTime;
import com.onemileshop.utils.UtiNet;

import android.os.AsyncTask;
import android.util.Log;
import android.util.Xml;

public class NetHomeTodayOrderList extends AsyncTask<ArrayList<EPostData>, Integer, EPostResult>
{

   private ITaskCallBack mCallBack;
   private EMsgHead mHead;

   public NetHomeTodayOrderList(EMsgHead head, ITaskCallBack callBack)
   {
	  mCallBack = callBack;
	  mHead = head;
   }
   
   @Override
   protected EPostResult doInBackground(ArrayList<EPostData>... params)
   {

	  try
	  {
		 EPostResult result = null;
		 HttpResponse response = UtiNet.HttpPostData2(UrlParams.PostURL, mHead, params[0]);
		 if (response != null)
		 {
			InputStream is = null;
			try
			{
			   is = response.getEntity().getContent();

			   XmlPullParser parser = Xml.newPullParser();
			   parser.setInput(is, "UTF-8");

			   EShopInfo user = null;
               ArrayList<EOrder> list=null;
               EOrder order=null;
			   int eventType = parser.getEventType();
			   while (eventType != XmlPullParser.END_DOCUMENT)
			   {
				  switch (eventType)
				  {
					 case XmlPullParser.START_TAG:// 开始元素事件

						String name = parser.getName();

						if (name.equalsIgnoreCase("Version"))
						{
						   result = new EPostResult();
						   result.setVersion(parser.nextText());
						}
						else if (name.equalsIgnoreCase("PostTime"))
						{
						   result.setPostTime(UtiDateTime.StringToDate((parser.nextText())));
						}
						else if (name.equalsIgnoreCase("APICode"))
						{
						   result.setAPICode(parser.nextText());
						}
						else if (name.equalsIgnoreCase("PageNo"))
						{
						   result.setPageNo(Integer.parseInt(parser.nextText()));
						}
						else if (name.equalsIgnoreCase("PageSize"))
						{
						   result.setPageSize(Integer.parseInt(parser.nextText()));
						}
						else if (name.equalsIgnoreCase("PageCount"))
						{
						   result.setPageCount(Integer.parseInt(parser.nextText()));
						}
						else if (name.equalsIgnoreCase("UseGZip"))
						{
						   result.setUseGZip(Integer.parseInt(parser.nextText()));
						}
						else if (name.equalsIgnoreCase("RspCode"))
						{
						   result.setRspCode(parser.nextText());
						}
						else if (name.equalsIgnoreCase("RspMsg"))
						{
						   result.setRspMsg(parser.nextText());
						   if (!result.getRspCode().equals("00000000"))
						   {
							  return result;
						   }
						}
						else if (name.equalsIgnoreCase("Orders"))
						{
						   list = new ArrayList<EOrder>();
						}
						else if (name.equalsIgnoreCase("OrderId"))
						{
						   order=new EOrder();
						   order.setOrderId(parser.nextText());
						}
						else if (name.equalsIgnoreCase("OrderName"))
						{
						   order.setOrderName(parser.nextText());
						}
						else if (name.equalsIgnoreCase("OrderNumber"))
						{
						   order.setOrderNumber(Integer.valueOf(parser.nextText()));
						}
						else if (name.equalsIgnoreCase("OrderAddress"))
						{
						   order.setOrderAddress(parser.nextText());
						}
						else if (name.equalsIgnoreCase("OrderMobile"))
						{
						   order.setOrderMobile(parser.nextText());
						}
						else if (name.equalsIgnoreCase("OrderType"))
						{
						   order.setOrderType(Integer.valueOf(parser.nextText()));
						}
						else if (name.equalsIgnoreCase("UserId"))
						{
						   order.setUserId(parser.nextText());
						}
						else if (name.equalsIgnoreCase("TotalPrice"))
						{
						   order.setTotalPrice(parser.nextText());
						}
						else if (name.equalsIgnoreCase("EarliestTime"))
						{
						   order.setEarliestTime(parser.nextText());
						}
						else if (name.equalsIgnoreCase("LatestTime"))
						{
						   order.setLatestTime(parser.nextText());
						   list.add(order);
						}
						break;
				  }
				  eventType = parser.next();
			   }
			   result.setResult(list);
			   return result;
			}
			catch (MalformedURLException e1)
			{
			   e1.printStackTrace();
			}
			catch (Exception e)
			{
			   if (Session.getInstance().IsDEBUG())
			   {
				  Log.e("ERROR", "NetAddressLoader_doInBackground()", e);
			   }
			   return null;
			}
			finally
			{
			   if (is != null)
			   {
				  try
				  {
					 is.close();
				  }
				  catch (IOException e)
				  {
					 if (Session.getInstance().IsDEBUG())
					 {
						Log.e("ERROR", "NetAddressLoader_doInBackground()_finally", e);
					 }
					 return null;
				  }
			   }
			}
		 }
		 else 
		 {
			return null;
		 }
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "NetPostData_doInBackground(params) ", e);
		 }
	  }
	  return null;
   }
   
   @Override
   protected void onPostExecute(EPostResult result)
   {
	  mCallBack.DoWhenComplete(result);
   }
   
   public interface ITaskCallBack
   {
	  public void DoWhenComplete(EPostResult result);
   }
}
